home *** CD-ROM | disk | FTP | other *** search
/ MacTech 1 to 12 / MacTech-vol-1-12.toast / Source / MacTech® Magazine / Volume 11 - 1995 / 11.02 Feb 95 / 11.02 Challenge / SolveMiddleLayer.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-12-10  |  3.9 KB  |  109 lines  |  [TEXT/KAHL]

  1. /*
  2.  
  3.     SolveRubiksCube
  4.     Copyright (c) 1994  J Robert Boonstra
  5.         
  6. */
  7.  
  8. #pragma options(honor_register,assign_registers)
  9.  
  10. #include "rubik.h"
  11. #include "transform.h"
  12.  
  13. /*
  14.  * Moves to transfer a cube to the middle face.
  15.  */
  16. /*      FDDLDlDDf  Up face matches Left center */
  17. #define FDDLDlDDf     F1;D2;L1;D1;L3;D2;F3;
  18.  
  19. #define RDDFDfDDr     R1;D2;F1;D1;F3;D2;R3;
  20.  
  21. #define BDDRDrDDb     B1;D2;R1;D1;R3;D2;B3;
  22.  
  23. #define LDDBDbDDl     L1;D2;B1;D1;B3;D2;L3;
  24.  
  25. Boolean  SolveMiddleLayer(register RubiksCube *rub)
  26. {
  27. short loopCount;
  28.   loopCount=0;
  29. /**********************************************************
  30.  * 
  31.  * STEP 3:  Put the (edge) cubes in the middle layer into  
  32.  *          the proper position and orientation.  
  33.  *          Loop until all are correct.
  34.  *
  35.  *********************************************************/
  36.   do {
  37.     if (++loopCount > 24) return false;
  38.     if (DF_F != D && DF_D != D) { /* DF in wrong position*/
  39.       if (DF_F == F && DF_D == R)        { D3F1D2L1D1L3D2F3;
  40.       } else if (DF_F == R && DF_D == F) { F1D2L1D1L3D2F3;
  41.       } else if (DF_F == R && DF_D == B) { R1D2F1D1F3D2R3;
  42.       } else if (DF_F == B && DF_D == R) { D1R1D2F1D1F3D2R3;
  43.       } else if (DF_F == B && DF_D == L) { D1B1D2R1D1R3D2B3;
  44.       } else if (DF_F == L && DF_D == B) { D2B1D2R1D1R3D2B3;
  45.       } else if (DF_F == L && DF_D == F) { D2L1D2B1D1B3D2L3;
  46.       } else if (DF_F == F && DF_D == L) { D3L1D2B1D1B3D2L3;
  47.       }
  48.       continue;
  49.     } else if (DR_R != D && DR_D != D) {/* DR in wrong pos*/
  50.       if (DR_R == F && DR_D == R)        { D2F1D2L1D1L3D2F3;
  51.       } else if (DR_R == R && DR_D == F) { D3F1D2L1D3L3D2F3;
  52.       } else if (DR_R == R && DR_D == B) { D3R1D2F1D1F3D2R3;
  53.       } else if (DR_R == B && DR_D == R) { R1D2F1D3F3D2R3;
  54.       } else if (DR_R == B && DR_D == L) { B1D2R1D1R3D2B3;
  55.       } else if (DR_R == L && DR_D == B) { D1B1D2R1D3R3D2B3;
  56.       } else if (DR_R == L && DR_D == F) { D1L1D2B1D1B3D2L3;
  57.       } else if (DR_R == F && DR_D == L) { D2L1D2B1D3B3D2L3;
  58.       }
  59.       continue;
  60.     } else if (DB_B != D && DB_D != D) {/* DB in wrong pos*/
  61.       if (DB_B == F && DB_D == R)        { D1F1D2L1D1L3D2F3;
  62.       } else if (DB_B == R && DB_D == F) { D2F1D2L1D3L3D2F3;
  63.       } else if (DB_B == R && DB_D == B) { D2R1D2F1D1F3D2R3;
  64.       } else if (DB_B == B && DB_D == R) { D3R1D2F1D3F3D2R3;
  65.       } else if (DB_B == B && DB_D == L) { D3B1D2R1D1R3D2B3;
  66.       } else if (DB_B == L && DB_D == B) { B1D2R1D3R3D2B3;
  67.       } else if (DB_B == L && DB_D == F) { L1D2B1D1B3D2L3;
  68.       } else if (DB_B == F && DB_D == L) { D1L1D2B1D3B3D2L3;
  69.       }
  70.       continue;
  71.     } else if (DL_L != D && DL_D != D) {/* DL in wrong pos*/
  72.       if (DL_L == F && DL_D == R)        { F1D2L1D1L3D2F3;
  73.       } else if (DL_L == R && DL_D == F) { D1F1D2L1D3L3D2F3;
  74.       } else if (DL_L == R && DL_D == B) { D1R1D2F1D1F3D2R3;
  75.       } else if (DL_L == B && DL_D == R) { D2R1D2F1D3F3D2R3;
  76.       } else if (DL_L == B && DL_D == L) { D2B1D2R1D1R3D2B3;
  77.       } else if (DL_L == L && DL_D == B) { D3B1D2R1D3R3D2B3;
  78.       } else if (DL_L == L && DL_D == F) { D3L1D2B1D1B3D2L3;
  79.       } else if (DL_L == F && DL_D == L) { L1D2B1D3B3D2L3;
  80.       }
  81.       continue;
  82.     }
  83. /*
  84.  * Exit if all edge cubes in the middle layer are in the
  85.  * correct position and orientation.
  86.  */  
  87.     else if (LF_F == F && RF_F == F &&
  88.              RF_R == R && RB_R == R &&
  89.              LB_B == B && RB_B == B &&
  90.              LF_L == L && LB_L == L) {
  91.       break;
  92.     } else {
  93. /*
  94.  * All edges are not correct, but there are no edge cubes
  95.  * in the bottom layer that belong in the middle layer.  
  96.  * Need to move an incorrectly placed cube from the middle
  97.  * layer into the bottom layer, so that the next loop can
  98.  * orient it correctly.
  99.  */
  100.       if (RF_F != F || RF_R != R)        { F1D2L1D1L3D2F3;
  101.       } else if (RB_R != R || RB_B != B) { R1D2F1D1F3D2R3;
  102.       } else if (LB_B != B || LB_L != L) { B1D2R1D1R3D2B3;
  103.       } else if (LF_L != L || LF_F != F) { L1D2B1D1B3D2L3;
  104.       }
  105.       continue;
  106.     }
  107.   } while(true);
  108.   return (true);
  109. }